package problem.year2022.Month08;
/**
 * @author LiangMu
 * @version \$Id: P30.java, v 0.1 2022年08月27日 12:39 下午 LiangMu Exp $$
 */
class P30 {
//    /**
//     * Definition for a binary tree node.
//     * public class TreeNode {
//     *     int val;
//     *     TreeNode left;
//     *     TreeNode right;
//     *     TreeNode() {}
//     *     TreeNode(int val) { this.val = val; }
//     *     TreeNode(int val, TreeNode left, TreeNode right) {
//     *         this.val = val;
//     *         this.left = left;
//     *         this.right = right;
//     *     }
//     * }
//     */
//    class Solution {
//        public int widthOfBinaryTree(TreeNode root) {
//
//        }
//    }
    static void main(String[] args) {
        def ints = [1, 2, 3]
        println ints.subList(0, 0)
        def index = ints.findLastIndexOf { it == 1 }
        println index
        println ints.subList(0, index)
    }

    static int widthOfBinaryTree(TreeNode root) {
        int maxLen = 0
        if (root == null) {
            return maxLen
        } else maxLen = 1
        def list = [root.left, root.right]
        return fun(list, maxLen)
    }

    static int fun(List<TreeNode> nodeList, int curLen) {
        int start = -1, end = -1
        boolean mark = true
        def len = nodeList.size()
        for (i in 0..<len) {
            if (nodeList[i] != null) {
                if (mark) {
                    start = i
                    end = i
                    mark = false
                } else end = i
            }
        }
        if (start == -1 || end == -1) return curLen
        def temp = end - start + 1
        curLen = Math.max(temp, curLen)
        nodeList = nodeList.subList(start, end + 1)
        List<TreeNode> paramList = new ArrayList<>()
        nodeList.each {paramList.addAll(it.left, it.right)}
        return fun(paramList, curLen)
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {}

    TreeNode(int val) { this.val = val; }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val
        this.left = left
        this.right = right
    }
}
